package com.nuanshui.heatedloan.controller.common;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nuanshui.framework.behavior.BehaviorContext;
import com.nuanshui.heatedloan.baseservice.facade.PictureConfigService;
import com.nuanshui.heatedloan.baseservice.model.SystemPicture;
import com.nuanshui.heatedloan.configBehavior.BehaviorUtil;
import com.nuanshui.heatedloan.configBehavior.ConfigBehavior;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.controller.BaseController;
import com.nuanshui.heatedloan.model.TaskList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * app配置加载类
 *
 * @author guohao
 * @date 2015年5月11日 上午10:20:31
 * @since 2.3.0
 */
@RestController
public class ConfigController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(getClass());


    /**
     * 根据屏幕尺寸获取图片配置
     *
     * @param
     * @throws InterruptedException
     * @author guohao
     * @date 2015年5月11日 上午11:06:33
     */
    @RequestMapping("loadConfig")
    public String loadConfig(String screenSize) throws InterruptedException {

        BehaviorContext.getContext().put("screenSize", screenSize);
        BehaviorContext.getContext().put("map", new HashMap<String, Object>());

        BehaviorUtil.doInvoke(ConfigBehavior.class);
        return success(BehaviorContext.getContext().get("map"));
    }

    @Reference(url = GlobalParam.BASESERVICE_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT, version = GlobalParam.BASESERVICE_DUBBO_SERVER_VERSION)
    private PictureConfigService pictureConfigService;

    /**
     * 加载任务列表
     */
    @RequestMapping("loadTaskList")
    public String loadTaskList(String screenSize) {
        // 获取启动页
        Map<String, Object> map = new HashMap<>();
        List<SystemPicture> launchImgUrls = pictureConfigService.findLaunchPicture(screenSize);
        if (launchImgUrls.size() > 0) {
            /**
             * 添加一个加载启动图任务
             */
            Map<String, Object> map1 = new HashMap<>();
            List<Object> obj = new ArrayList<>();

            List<TaskList> tasks = new ArrayList<>();

            int nSize = launchImgUrls.size();
            for (int i = 0; i < nSize; i++) {
                TaskList task = new TaskList();
                SystemPicture item = launchImgUrls.get(i);
                task.setJumpUrl(item.getAccessUrl());

                if (item.getShowAt() != null) {
                    task.setShowAt(item.getShowAt().getTime());
                }
                task.setUpdatedAt(item.getCreatedAt().getTime());
                task.setLaunchImgUrl(item.getLaunchImageUrl());
                tasks.add(task);
            }
            map1.put("type", 1);
            map1.put("data", tasks);
            obj.add(map1);


            map.put("timeInterval", 5 * 60 * 1000);
            map.put("taskList", obj);
        }
        return success(map);
    }
}
